package com.ruoyi.canteen.mapper;

import java.util.List;
import com.ruoyi.canteen.domain.CanteenCpOrderReturn;
import com.ruoyi.canteen.vo.OrderShowVo;
import org.apache.ibatis.annotations.*;

/**
 * 订单退货Mapper接口
 * 
 * @author ruoyi
 * @date 2022-04-06
 */
public interface CanteenCpOrderReturnMapper 
{
    /**
     * 查询订单退货
     * 
     * @param id 订单退货主键
     * @return 订单退货
     */
    public CanteenCpOrderReturn selectCanteenCpOrderReturnById(Long id);

    /**
     * 查询订单退货列表
     * 
     * @param canteenCpOrderReturn 订单退货
     * @return 订单退货集合
     */
    public List<CanteenCpOrderReturn> selectCanteenCpOrderReturnList(CanteenCpOrderReturn canteenCpOrderReturn);

    /**
     * 新增订单退货
     * 
     * @param canteenCpOrderReturn 订单退货
     * @return 结果
     */
    public int insertCanteenCpOrderReturn(CanteenCpOrderReturn canteenCpOrderReturn);

    /**
     * 修改订单退货
     * 
     * @param canteenCpOrderReturn 订单退货
     * @return 结果
     */
    public int updateCanteenCpOrderReturn(CanteenCpOrderReturn canteenCpOrderReturn);

    /**
     * 删除订单退货
     * 
     * @param id 订单退货主键
     * @return 结果
     */
    public int deleteCanteenCpOrderReturnById(Long id);

    /**
     * 批量删除订单退货
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCanteenCpOrderReturnByIds(Long[] ids);

    /**
     * 更新订单状态
     * @CanteenCpOrderReturn 订单退款记录
     * @return
     */
    @Update("update canteen_cp_order_return set return_checker_id=#{returnCheckerId}," +
            "return_apply_status=#{orderStatus}, check_time=#{checkTime}," +
            "return_check_content#{returnCheckContent},update_by=#{updateBy},update_time=#{updateTime}" +
            "where order_id= #{orderId}")
    public int updateReturnOrder(CanteenCpOrderReturn canteenCpOrderReturn);


    /**
     * 查询当前用户关联门店的退单列表(当日订单)
     * @param mdIds 门店ids
     * @return
     */
    @Select("select o.id,o.order_no,order_type,o.md_id,total_price,r.return_reason, " +
            "order_status,o.create_time,o.deliver_link_man,o.phone,o.address, m.md_name from " +
            "(SELECT * from canteen_cp_order " +
            "where id in(SELECT order_id from canteen_cp_order_return " +
//            "where md_id in(#{mdIds}) and date(apply_time) = curdate() ) " +
            "where md_id in(${mdIds}) and DATE_SUB(curdate(), INTERVAL 2 DAY) <= date(apply_time) ) " +
            ") as o left join canteen_md as m on o.md_id = m.md_id " +
            "left join canteen_cp_order_return as r on o.id = r.order_id " +
            "order by o.create_time desc")
    @Results({
            @Result(property = "orderId", column = "id"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "mdId", column = "md_id"),
            @Result(property = "totalPrice", column = "total_price"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "dateAdd", column = "create_time"),
            @Result(property = "mdName", column = "md_name"),
            @Result(property = "mdPic", column = "md_pic"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "deliverLinkMan", column = "deliver_link_man"),
            @Result(property = "address", column = "address"),
            @Result(property = "returnReason", column = "return_reason")
    })
    public List<OrderShowVo> selectDefaultOrderShowVoList(@Param("mdIds") String mdIds);

    /**
     * 微信支付退款：用户确认支付，订单已完成
     * 更新：支付状态为已支付+订单状态为已完成
     * @param openid  用户openid
     * @param orderNo 订单编号
     * @param serialNo 微信支付成功返回值
     * @return
     */
    @Update("update canteen_cp_order set order_status=12,serial_no=#{serialNo} " +
            "where pay_flag=1 and order_no=#{orderNo} and " +
            "user_id = (SELECT id FROM canteen_user where openid=#{openid} and delete_time is null)")
    public int confirmPayByOpenId(@Param("openid") String openid,
                                  @Param("orderNo") String orderNo, @Param("serialNo") String serialNo);
}
